using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Elementum
{
    class ContactManager : Constant,IContactListener
    {
        //protected List<IContactListener> _contactListener=new List<IContactListener>();

        public ContactManager() { }

        //public List<IContactListener> ContactListener {
        //    get { return _contactListener; }
        //}

        public void BeginContact(Contact contact)
        {
            Vector2 angVector2;
            WorldManifold w;
            contact.GetWorldManifold(out w);
            angVector2 = w._normal;

            float normal = (float)Math.Atan2(-angVector2.Y, angVector2.X) * 180 / PI;
            normal += 360;
            normal = normal % 360;

            if (contact.GetFixtureA().GetBody().BeginContact != null)
                contact.GetFixtureA().GetBody().BeginContact(contact, contact.GetFixtureB().GetBody(), normal);

            if (contact.GetFixtureB().GetBody().BeginContact != null)
                contact.GetFixtureB().GetBody().BeginContact(contact, contact.GetFixtureA().GetBody(), (normal + 180) % 360);

            //foreach (IContactListener c in _contactListener)
            //    c.BeginContact(contact);
            //throw new NotImplementedException();
        }

        public void EndContact(Contact contact)
        {
            Vector2 angVector2;
            WorldManifold w;
            contact.GetWorldManifold(out w);
            angVector2 = w._normal;

            float normal = (float)Math.Atan2(-angVector2.Y, angVector2.X) * 180 / PI;
            normal += 360;
            normal = normal % 360;

            if (contact.GetFixtureA().GetBody().EndContact != null)
                contact.GetFixtureA().GetBody().EndContact(contact, contact.GetFixtureB().GetBody(), normal);

            if (contact.GetFixtureB().GetBody().EndContact != null)
                contact.GetFixtureB().GetBody().EndContact(contact, contact.GetFixtureA().GetBody(), (normal+180)%360);
            //foreach (IContactListener c in _contactListener)
            //    c.EndContact(contact);
        }

        public void PreSolve(Contact contact, ref Manifold oldManifold)
        {
            //foreach (IContactListener c in _contactListener)
            //    c.PreSolve(contact, ref oldManifold);
        }

        public void PostSolve(Contact contact, ref ContactImpulse impulse)
        {
            //foreach (IContactListener c in _contactListener)
            //    c.PostSolve(contact, ref impulse);
        }
    }
}
